package com.itany.shop.service.impl;

import com.itany.shop.dao.ProductDao;
import com.itany.shop.entity.Item;
import com.itany.shop.entity.Product;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.util.ParameterUtil;
import com.itany.shop.vo.Cart;

import java.util.Iterator;
import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class CartServiceImpl implements CartService {
    //调用持久层
    private ProductDao productDao = (ProductDao) ObjectFactory.getObject("productDao");

    @Override
    public void addCart(Cart cart, String id, Integer num) throws UserNotLoginException, RequestParameterErrorException {
        if (cart == null) {
            throw new UserNotLoginException("请先登录");
        }
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Product product = productDao.selectById(Integer.parseInt(id));
        List<Item> cartItems = cart.getItems();
        //看商品是否在购物车中
        boolean isExist = false;
        //遍历购物车
        for (Item item : cartItems) {
            if (item.getProduct().getId().equals(product.getId())) {
                //存在商品，更新
                item.setNum(item.getNum() + num);//数量
                item.setPrice(product.getPrice() * item.getNum());//产品单价成数量
                isExist = true;
                break;
            }
        }
        //不存在，创建新的购物项并加入
        if (!isExist) {
            Item newItem = new Item();
            newItem.setProduct(product);
            newItem.setNum(num);
            newItem.setPrice(product.getPrice() * num);
            cartItems.add(newItem);
        }
        //统计购物车价格
        double totalPrice = 0.0;
        for (Item item : cartItems) {
            totalPrice += item.getPrice();
        }
        cart.setPrice(totalPrice);//更新总价
        System.out.println("商品：" + product.getName() + "\t数量:" + num + "\t小计:" + (product.getPrice() * num));
        System.out.println("当前购物车总金额：" + cart.getPrice());

    }

    @Override
    public void removeCart(Cart cart, String id) throws UserNotLoginException, RequestParameterErrorException {
        if (cart == null) {
            throw new UserNotLoginException("请先登录");
        }
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        List<Item> items = cart.getItems();
        boolean removed = false;
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            Item item = iterator.next();
            // 判断商品ID是否匹配
            if (item.getProduct() != null && item.getProduct().getId().equals(Integer.parseInt(id))) {
                iterator.remove();
                removed = true;
            }
        }
        if (removed) {
            double total = 0.0;
            for (Item item : cart.getItems()) {
                total += item.getPrice();
            }
            cart.setPrice(total);
        } else {
            System.out.println("商品ID不存在！");
        }
    }

    @Override
    public void updateCart(Cart cart, String id,Integer newNum) throws UserNotLoginException, RequestParameterErrorException {
        if (cart == null) {
            throw new UserNotLoginException("请先登录");
        }
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        for (Item item : cart.getItems()) {
            if (item.getProduct().getId().equals(Integer.parseInt(id))) {
                // 更新商品数量
                item.setNum(newNum);
                // 计算并更新小计
                item.setPrice(item.getProduct().getPrice() * newNum);
                break;
            }
        }
        double total = 0.0;
        for (Item item : cart.getItems()) {
            total += item.getPrice();
        }
        cart.setPrice(total);
    }
}
